package com.example.cloudmaps;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MapDataSource {
	
	private SQLiteDatabase db;
	private MapSQLiteHelper dbHelper;
	
	//All the columns in the users table
		private String[] allUsers = {
				MapSQLiteHelper.COLUMN_USER,
				MapSQLiteHelper.COLUMN_PASSWORD
		};
		
		/*
		 * Constructor
		 */
		public MapDataSource(Context context){
			dbHelper = new MapSQLiteHelper(context);
		}
		
		/*
		 * DB operations
		 */
		public void open() throws SQLException{
			db = dbHelper.getWritableDatabase();
		}
		
		public void close(){
			dbHelper.close();
		}
		
		//Query Functions
		
		//Update User info in DB
		public void updateUser(String username, String password) throws Exception{
			String USERNAME_UPDATE = "UPDATE users SET username= " + "'" + username + "'" + " WHERE uid = 1";
			String PASSWORD_UPDATE = "UPDATE users SET password= " + "'" + password + "'" + " WHERE uid = 1";
			
			db.execSQL(USERNAME_UPDATE);
			db.execSQL(PASSWORD_UPDATE);

		}
		
		public void logoutUser() throws Exception{
			String USERNAME_UPDATE = "UPDATE users SET username= 'user' WHERE uid = 1";
			String PASSWORD_UPDATE = "UPDATE users SET password= 'pass' WHERE uid = 1";
			
			db.execSQL(USERNAME_UPDATE);
			db.execSQL(PASSWORD_UPDATE);

		}
		
		public String getUsername(){
			Cursor c = db.query(MapSQLiteHelper.TABLE_USERS, allUsers, MapSQLiteHelper.COLUMN_USER_ID + " = 1", null, null, null, null);
			if(c.getCount() == 0){
				Log.d("CM", "Couldn't find user ");
				return null;
			}
			c.moveToFirst();
			return c.getString(0);
		}
		

}
